<template>
  <div>
    <el-button class="mb5" style="float: right" type="primary" @click="getList">
      <template #icon>
        <i-ep-search/>
      </template>
      刷新
    </el-button>
    <el-table v-adaptive="520" :border="true" :data="labApplyList" :header-cell-style="$headerCellStyle" :stripe="true"
              highlight-current-row>
      <el-table-column fixed="left" label="明细" type="expand">
        <template #default="props">
          <div m="4">
            <el-table :data="props.row.detailVoList" :stripe="true" highlight-current-row>
              <el-table-column fixed="left" width="100px"/>
              <el-table-column fixed="left" label="项目代码" prop="itemCode" width="150px"/>
              <el-table-column fixed="left" label="项目名称" prop="itemName" width="180px"/>
              <el-table-column fixed="left" label="数量" prop="num" width="120px"/>
              <el-table-column fixed="left" label="单价(元)" prop="price" width="120px"/>
              <el-table-column fixed="left" label="总价(元)" prop="sumPrice" width="120px"/>
              <el-table-column :align="'center'" :show-overflow-tooltip="true" class-name="small-padding fixed-width"
                               fixed="right"
                               label="操作" width="70">
                <template #default="scope">
                  <el-tooltip class="item" content="删除" effect="dark" placement="top">
                    <el-button circle type="danger"
                               @click="handleDelete(scope.row)">
                      <template #icon>
                        <i-ep-delete/>
                      </template>
                    </el-button>
                  </el-tooltip>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </template>
      </el-table-column>
      <el-table-column v-if="isQuoted" :align="'center'" label="模板主题名称" min-width="150" prop="title">
        <template #default="scope">
          <span class="ys-css">{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="检验类型" prop="itemTypeName"/>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="症状" prop="symptom" width="300"/>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="其他诊断" prop="relevantDiag"
                       width="300"/>
      <el-table-column :align="'center'" label="体征" prop="sign" width="300"/>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" class-name="small-padding fixed-width"
                       fixed="right"
                       label="操作" width="70">
        <template #default="scope">
          <el-tooltip v-if="!isQuoted" class="item" content="删除" effect="dark" placement="top">
            <el-button circle type="danger"
                       @click="handleDelete(scope.row)">
              <template #icon>
                <i-ep-delete/>
              </template>
            </el-button>
          </el-tooltip>
          <el-tooltip v-else class="item" content="引用" effect="dark" placement="top">
            <el-button circle type="success"
                       @click="templateQuote(scope.row)">
              <svg-icon icon-class="save"/>
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script lang="ts" setup>

import {delItemIdenticalGroup, getGroupApplyList} from "@/api/patient/templateDetail.ts";

const emits = defineEmits(['rowClick']);
const props = defineProps({
  orgId: {
    type: String,
    default: ""
  },
  // 是否从引用加载
  isQuoted: {
    type: Boolean,
    default: false
  }
})

const {proxy} = getCurrentInstance() as any;
const queryParamsRef = ref();
const queryParams = ref<any>({
  type: '4',
  orgId: props.orgId,
  // 是否通过分组后的记录id获取明细
  isAddItem: "Y"
});
const labApplyList = ref([]);
const open = ref(false);

function getList() {
  getGroupApplyList(queryParams.value).then((response: any) => {
    labApplyList.value = response.data;
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const param = {
    itemIdenticalGroup: row.itemIdenticalGroup,
    templateMasterId: row.templateMasterId,
    detailId: row.detailId
  };
  ElMessageBox.confirm("是否确认删除？", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delItemIdenticalGroup(param).then(() => {
        ElMessage.success("删除成功");
        getList();
      });
    })
    .catch(() => {
    });
}

/**
 * 引用模板数据
 * @param row - 包含模板明细数据的对象
 */
function templateQuote(row) {
  const selectedLabList: any = [];
  row.detailVoList.forEach((detailRow: any) => {
    // 创建检查项目对象
    const labClinicCheckObj = {
      vsId: detailRow.vsId,
      "amount": Number(detailRow.num),
      "clinicId": detailRow.clinicItemId,
      "clinicItemId": detailRow.clinicItemId,
      "isShowType": true,
      "itemCode": detailRow.itemCode,
      "itemName": detailRow.itemName,
      "itemType": detailRow.itemType,
      "labId":detailRow.itemType,
      "labTube": detailRow.labTube[0],
      "labTubeName": detailRow.labTube[1],
      "labType": detailRow.itemType,
      "labTypeName": detailRow.itemTypeName,
      "num": Number(detailRow.num),
      "orgId": props.orgId,
      "price": Number(detailRow.price),
      "totalPrice": Number(detailRow.totalPrice),
      "remarks": detailRow.remarks,
      "specimen": detailRow.specimen[0],
      "specimenName": detailRow.specimen[1]
    };
    selectedLabList.push(labClinicCheckObj);
  })
  const form = {
    isUrgent: 'N',
    symptom: row.detailVoList[0].symptom,
    relevantDiag: row.detailVoList[0].relevantDiag,
    sign: row.detailVoList[0].sign,
    remarks: row.detailVoList[0].remarks
  };
  emits("rowClick", {form, selectedLabList});
}

defineExpose({
  getList
})
</script>

<style scoped>

</style>
